﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using GMap.NET.WindowsPresentation;

namespace GisControl.CustomMarkers
{
    /// <summary>
    /// Interaction logic for CustomMarkerDemo.xaml
    /// </summary>
    public partial class SurveillanceCameraMarker
    {
        private Popup m_popup;
        private Label m_label;
        private readonly GMapMarker m_marker;
        private GMapControl m_mapControl;

        public SurveillanceCameraMarker(GMapControl window, GMapMarker marker, string title)
        {
            this.InitializeComponent();

            this.m_mapControl = window;
            this.m_marker = marker;

            m_popup = new Popup();
            m_label = new Label();

            this.Loaded += new RoutedEventHandler(CustomMarkerDemo_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(CustomMarkerDemo_SizeChanged);
            this.MouseEnter += new MouseEventHandler(MarkerControl_MouseEnter);
            this.MouseLeave += new MouseEventHandler(MarkerControl_MouseLeave);
            this.MouseMove += new MouseEventHandler(CustomMarkerDemo_MouseMove);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonUp);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomMarkerDemo_MouseLeftButtonDown);

            m_popup.Placement = PlacementMode.Mouse;
            {
                m_label.Background = Brushes.Blue;
                m_label.Foreground = Brushes.White;
                m_label.BorderBrush = Brushes.WhiteSmoke;
                m_label.BorderThickness = new Thickness(2);
                m_label.Padding = new Thickness(5);
                m_label.FontSize = 22;
                m_label.Content = title;
            }
            m_popup.Child = m_label;
        }

        private void CustomMarkerDemo_Loaded(object sender, RoutedEventArgs e)
        {
            if (icon.Source.CanFreeze)
            {
                icon.Source.Freeze();
            }
        }

        private void CustomMarkerDemo_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            m_marker.Offset = new Point(-e.NewSize.Width / 2, -e.NewSize.Height);
        }

        private void CustomMarkerDemo_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured)
            {
                Point p = e.GetPosition(m_mapControl);
                m_marker.Position = m_mapControl.FromLocalToLatLng((int)p.X, (int)p.Y);
            }
        }

        private void CustomMarkerDemo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsMouseCaptured)
            {
                Mouse.Capture(this);
            }
        }

        private void CustomMarkerDemo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
            {
                Mouse.Capture(null);
            }
        }

        private void MarkerControl_MouseLeave(object sender, MouseEventArgs e)
        {
            m_marker.ZIndex -= 10000;
            m_popup.IsOpen = false;
        }

        private void MarkerControl_MouseEnter(object sender, MouseEventArgs e)
        {
            m_marker.ZIndex += 10000;
            m_popup.IsOpen = true;
        }
    }
}